<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="风险标题" prop="riskTitle">
        <el-input
          v-model="queryParams.riskTitle"
          placeholder="请输入项目风险标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目" clearable>
          <el-option v-for="project in projectList"
                     :label="project.projectName"
                     :value="project.projectId"
                     :key="project.projectId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:projectRisk:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:projectRisk:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:projectRisk:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectRiskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="项目风险主键" align="center" prop="riskId"/>
      <el-table-column label="风险类别名称" align="center" prop="risktypeName"/>
      <el-table-column label="风险标题" align="center" prop="riskTitle"/>
      <el-table-column label="风险内容" align="center" prop="riskContent"/>
      <el-table-column label="跟进人" align="center" prop="nickName"/>
      <el-table-column label="状态" align="center" prop="riskStatus">
        <!-- 状态,1-待跟进,2-跟进中,3-已解决 -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.riskStatus === 1" type="success">待跟进</el-tag>
          <el-tag v-if="scope.row.riskStatus === 2" type="warning">跟进中</el-tag>
          <el-tag v-if="scope.row.riskStatus === 3" type="danger">已解决</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" align="center" prop="projectName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:projectRisk:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:projectRisk:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目风险对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目" clearable>
            <el-option v-for="project in projectList"
                       :label="project.projectName"
                       :value="project.projectId"
                       :key="project.projectId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险类别" prop="risktypeId">
          <el-select v-model="form.risktypeId" placeholder="请选择风险类别" clearable>
            <el-option v-for="risktype in risktypeList"
                       :label="risktype.risktypeName"
                       :value="risktype.risktypeId"
                       :key="risktype.risktypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险标题" prop="riskTitle">
          <el-input v-model="form.riskTitle" placeholder="请输入项目风险标题"/>
        </el-form-item>
        <el-form-item label="跟进人" prop="userId">
          <div style="display: flex">
            <el-input v-model="currentChooseUser.label" disabled/>
            <el-button style="margin-left:10px;" plain type="primary" size="mini" @click="chooseUser()">
              选择跟进人
            </el-button>
          </div>
        </el-form-item>
        <el-form-item label="风险状态" prop="riskStatus">
          <el-radio v-model="form.riskStatus" label="1">待跟进</el-radio>
          <el-radio v-model="form.riskStatus" label="2">跟进中</el-radio>
          <el-radio v-model="form.riskStatus" label="3">已解决</el-radio>
        </el-form-item>
        <el-form-item label="风险内容">
          <el-input type="textarea" v-model="form.riskContent" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择人员弹框 -->
    <el-dialog :title="chooseUserTitle" :visible.sync="chooseUserOpen" width="500px">
      <ChooseUser :projectId="form.projectId" @getData="getData"/>
      <el-button type="primary" @click="handleSelect">确定</el-button>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProjectRisk,
  getProjectRisk,
  delProjectRisk,
  addProjectRisk,
  updateProjectRisk
} from "@/api/project/projectRisk";
import {listProjectAllByUserId} from "@/api/project/project";
import {listAllRiskType} from "@/api/project/riskType";
import ChooseUser from "@/components/ChooseUser/index.vue";

export default {
  name: "ProjectRisk",
  components: {ChooseUser},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目风险表格数据
      projectRiskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        riskTitle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 项目列表
      projectList: [],
      // 风险类别
      risktypeList: [],
      // 选择人员弹框标题
      chooseUserTitle: '选择人员',
      // 选择人员弹框开关
      chooseUserOpen: false,
      // 当前选择人员
      currentChooseUser: {},
    };
  },
  created() {
    this.getList();
    this.getProjectList();
    this.getRisktypeList();
  },
  methods: {
    /** 查询项目风险列表 */
    getList() {
      this.loading = true;
      listProjectRisk(this.queryParams).then(response => {
        this.projectRiskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取项目列表 */
    getProjectList() {
      listProjectAllByUserId().then(respnse => {
        this.projectList = respnse.data;
      }).catch(() => {
      });
    },
    /** 获取风险类别列表 */
    getRisktypeList() {
      listAllRiskType().then(response => {
        this.risktypeList = response.data;
      }).catch(() => {
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riskId: null,
        projectId: null,
        risktypeId: null,
        riskTitle: null,
        userId: null,
        riskStatus: null,
        riskContent: null
      };
      this.currentChooseUser = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riskId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目风险";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const riskId = row.riskId || this.ids
      getProjectRisk(riskId).then(response => {
        this.form = response.data;
        this.currentChooseUser.label = response.data.nickName;
        this.form.riskStatus = this.form.riskStatus + "";
        this.open = true;
        this.title = "修改项目风险";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.riskStatus = Number(this.form.riskStatus)
          if (this.form.riskId != null) {
            updateProjectRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riskIds = row.riskId || this.ids;
      this.$modal.confirm('是否确认删除项目风险编号为"' + riskIds + '"的数据项？').then(function () {
        return delProjectRisk(riskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectRisk/export', {
        ...this.queryParams
      }, `projectRisk_${new Date().getTime()}.xlsx`)
    },
    /** 获取选择人员 */
    getData(val){
      this.currentChooseUser = val;
    },
    /** 确认选择 */
    handleSelect(){
      this.form.userId = this.currentChooseUser.id;
      this.chooseUserOpen = false;
    },
    /** 选择人员 */
    chooseUser(){
      // 判断 projectId 是否是 Number类型
      if (typeof this.form.projectId !== 'number') {
        this.$modal.msgError("请选择项目");
        return;
      }
      this.chooseUserOpen = true;
    },

  }
};
</script>
